//
//  LKRequestErrorHandler.m
//  Live
//
//  Created by 唐开福 on 2017/9/6.
//  Copyright © 2017年 LaKa. All rights reserved.
//

#import "LKRequestLocalCodeHandler.h"
#import "BNDOfficeDocKit.h"

@implementation LKRequestLocalCodeHandler

+ (NSError *)localizeErrorWithError:(NSError *)error
{
    LKNetworkConnectErrorCode statusCode = LK_NETWORK_ERROR_DEFAULT;
    
    // 连接不上服务器
    if(error.code == NSURLErrorCannotConnectToHost) {
        
        statusCode = LK_NETWORK_ERROR_CONNECT_ERROR;
        
    // 请求超时
    }else if (error.code == NSURLErrorTimedOut) {
        
        statusCode = LK_NETWORK_ERROR_REQUEST_TIMEOUT;
        
    // JSON 解析失败
    }else if (error.code == NSPropertyListReadCorruptError || error.code == NSPropertyListReadCorruptError) {
        
        statusCode = LK_NETWORK_ERROR_DATA_INVALID;
        
    // 取消请求
    }else if (error.code == NSURLErrorCancelled) {
        
        statusCode = LK_NETWORK_ERROR_USER_CANCEL;
        
    // 没有网络
    }else if (error.code == NSURLErrorNotConnectedToInternet || error.code == NSURLErrorNetworkConnectionLost) {
        
        statusCode = LK_NETWORK_ERROR_NOT_NETWORK;
        
    // 接口返回错误500
    }else if(error.code == NSURLErrorBadServerResponse) {
        
        statusCode = LK_NETWORK_ERROR_DATA_INVALID;
    }
    
    NSString *msgString = [self errorDescriptionWithErrorCode:statusCode];
    NSError *cError = [NSError errorWithDomain:@"com.laka.live.API"
                                          code:statusCode
                                      userInfo:@{NSLocalizedDescriptionKey : TTStringNotNil(msgString)}];
    return cError;
}

+ (NSString *)errorDescriptionWithErrorCode:(LKNetworkConnectErrorCode)errorCode
{
    NSString *msgString = @"暂无内容";
    switch (errorCode) {
            
        case LK_NETWORK_ERROR_DEFAULT: {
            msgString = @"网络异常，稍后再试";
            break;
        }
        case LK_NETWORK_ERROR_USER_CANCEL: {
            
            msgString = nil;
            break;
        }
        case LK_NETWORK_ERROR_REQUEST_TIMEOUT: {
            
            msgString = @"请求超时，稍后再试";
            break;
        }
        case LK_NETWORK_ERROR_NOT_NETWORK: {
            
            msgString = @"没有网络，请检查网络";
            break;
        }
        case LK_NETWORK_ERROR_DATA_INVALID: {
            
            msgString = @"返回数据异常";
            break;
        }
        case LK_NETWORK_ERROR_CONNECT_ERROR: {
            
            msgString =  @"连接不上，稍后再试";
            break;
        }
        case LK_NETWORK_ERROR_RETURN_DATA_NULL: {
            
            msgString = @"返回数据异常";
            break;
        }
    }
    return msgString;
}

+ (BOOL)isNetworkConnectError:(NSError *)error
{
    return [self isNetworkConnectErrorCode:error.code];
}

+ (BOOL)isNetworkConnectErrorCode:(NSInteger)errorCode
{
    if(errorCode <= LK_NETWORK_ERROR_RETURN_DATA_NULL || errorCode >= LK_NETWORK_ERROR_USER_CANCEL ) {
        return YES;
    }else {
        return NO;
    }
}
@end
